<?php
namespace app\admin\controller;
use app\BaseController;
use app\service\commonService;
use auth\Auth;
use think\facade\Db;
use think\Request;

class Common extends BaseController
{
    public function initialize(){
        $request=request();
        //获取当前操作控制器名称和方法名称

        $con = $request->controller();
        $action = $request->action();
        $name = $con .'/'. $action;
        //echo $name;
        if($name !== 'Bill/notice' && $name !== 'Index/superedit' && $name !== 'Index/filedel' && $name !== 'Bill/wxorder' && $name !== 'Bill/verification' && $name !== 'Roominfo/fetchdata'&& $name !== 'Bill/getbillmsg'){
            if(!session('id') || !session('name')){
                $url=url('admin/login/login');
                $this->error('您尚未登录系统',$url);

            } 
        }

        //$auth = (new Auth())->check($name,session('id'));
        $auth=true;
        //可以直接访问的权限
        $notCheck = array('Index/index','Index/welcome','Index/transaction','Index/passwordedit','Bill/notice','Bill/wxorder','Bill/verification','Roominfo/fetchdata','Admin/fetchdata','Index/superedit','Index/filedel','Bill/getbillmsg','Roominfo/getcosttype','Billcheck/getdata');
        //超级管理员可以访问所有权限
        if(session('usertype')!=="0"){
            if(!in_array($name,$notCheck)){
                if(!$auth){
                   $this->error('没有权限访问',url('index/welcome'));
                }
                
            }
        }
    }

    //根据当前账号信息查询token
    public function pidTokenId(){
        $id = session('id');
        //根据ID查询用户信息
        $adminId = (new commonService())->adminIdInfo($id);
        $usertype = $adminId['usertype'];//角色类型
        session("usertype",$usertype);
        $department = $adminId['department'];//所属部门
        if($usertype == 0){//超级管理员
            //查询所有服务商
            
            $com['service_id'] = (new commonService())->serviceList();
            //查询所有物业
            $com['property_id'] = (new commonService())->propertyList();
            //查询所有小区
            $com['community_id'] = (new commonService())->communityList();
            $com["usertype"]=0;
            $com['com_id'] = Db::name('community')->column('id');;
        }else if($usertype == 1){//服务商
            $com['service_id'] = $department;
            $com["usertype"]=1;
            //查询所有物业
            $com['property_id'] = (new commonService())->serviceIdProperty($department);
            //查询所有小区
            $com['community_id'] = (new commonService())->propertyIdCommunity($com['property_id']);
            $com['com_id']= Db::name('community')->where('property_id','in',$com['property_id'])->column('id');
        }else if($usertype == 2){//物业
            //查询所有小区
            $com["usertype"]=2;
            $com['property_id'] = $department;
            $com['community_id'] = (new commonService())->propertyIdCommunity($department);
            $com['com_id']= Db::name('community')->where('property_id','in',$department)->column('id');
        }else if($usertype == 3){//小区
            $com["usertype"]=3;
            $com['community_id'] = (new commonService())->communityIdCommunity($department);
            $com['com_id']=Db::name('community')->where('id',$department)->value('id');
        }
        return $com;
    }
}